home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
LIFER__
/
PROTO
/
U
/
COMMON_L.C
< prev
next >
Wrap
Text File
|
1991-08-02
|
5KB
|
208 lines
/* Common_Life */ /* Common */
/* Unit name: Common_Life.c */
/* Function: Common variables for program specific code. */
/* History: 7/23/91 Original by Prototyper 3.0 */
#include "PCommonLife.h" /* Common */
#include "Common_Life.h" /* Common */
struct table
{
char lines[11];/* USE VALUES 1 TO 10 */
};
static struct table new_display_table[6],display_table[6] = /* USE VALUES 1 TO 5 */
{{' ','-','-','-','-','-','-','-','-','-','-'},/* SKIP */
{' ','-','-','-','-','-','-','-','-','-','-'},/* 1 */
{' ','-','-','-','-','-','-','-','-','-','-'},
{' ','-','-','-','-','-','-','-','-','-','-'},
{' ','-','-','-','-','-','-','-','-','-','-'},
{' ','-','-','-','-','-','-','-','-','-','-'}};/* 5 */
struct table *pdisplay_table = display_table;
struct table *pnew_display_table = new_display_table;
/* display_table is used for the grid's output. */
/* - = dead * = alive */
StringHandle gDefaultTEX, gDefaultTEY;
long gX_Value, gY_Value, gCellCounter = 1;
/* ======================================================= */
Boolean CHECKXY(GetSelection, DType, DItem, tempRect)
DialogPtr GetSelection;
short DType;
Handle DItem;
Rect tempRect;
{
Str255 sTemp;
/* CHECK X FIRST */
GetDItem(GetSelection, InTEBoxX, &DType, &DItem, &tempRect);/* Get item information */
GetIText(DItem, &sTemp);
StringToNum(sTemp, &gX_Value);
/* CHECK Y */
GetDItem(GetSelection, InTEBoxY, &DType, &DItem, &tempRect);/* Get item information */
GetIText(DItem, &sTemp);
StringToNum(sTemp, &gY_Value);
if ((gX_Value < 1 || gX_Value > MaxColumns) || (gY_Value < 1 || gY_Value > 5))
{
PA_Life_Alert(); /* SHOW ALERT WINDOW */
return(0);
}
return(1); /* 1 = VALUES FOR X & Y ARE GOOD */
}
/* ======================================================= */
void STORE_VALUES(GetSelection, DType, DItem, tempRect)
DialogPtr GetSelection;
short DType;
Handle DItem;
Rect tempRect;
{
/* STORE THE ARRAY xxx*/
display_table[gY_Value].lines[gX_Value] = '*';
/* CHECK TO SEE IF IT'S MORE THAN 10 CELLS */
/* IF NOT INCREASE AND DISPLAY COUNTER */
if (gCellCounter < MaxColumns)
{
/* INCREASE THE CELL COUNTER */
gCellCounter++;
/* UPDATE CELL COUNTER IN DISPLAY */
NumToString(gCellCounter, sTemp);
GetDItem(GetSelection,CellBox, &DType, &DItem, &tempRect);
SetIText(DItem,sTemp);
}
}
/* ======================================================= */
/* DISPLAYS & COMPUTES GENERATIONS */
void LIFE_PROGRAM()
{
DRAWNSTRING();
COMPUTE();
}
/* ======================================================= */
void COMPUTE(void)
{
int counterY,counterX;
for(counterY=1;counterY <= MaxRows;counterY++)
{
for(counterX=1;counterX <= MaxColumns;counterX++)
{
switch(NEIGHBORCOUNT(counterY,counterX))
{
case 0:
case 1:
(pnew_display_table + counterY)->lines[counterX] = '-';
break;
case 2:
(pnew_display_table + counterY)->lines[counterX] = (pdisplay_table + counterY)->lines[counterX];
break;
case 3:
(pnew_display_table + counterY)->lines[counterX] = '*';
break;
case 4:
case 5:
case 6:
case 7:
case 8:
(pnew_display_table + counterY)->lines[counterX] = '-';
break;
}/* switch */
}/* for counterX */
}/* for counterY*/
CopyMap();
}
/* ======================================================= */
int NEIGHBORCOUNT(int countY, int countX)
{
int i,j;
int rlow,rhigh;
int clow, chigh;
int count = 0;
if (countY <= 0)
rlow = 0;
else
rlow = countY - 1;
if (countY >= MaxRows - 1)
rhigh = MaxRows - 1;
else
rhigh= countY + 1;
if (countX <= 0)
clow = 0;
else
clow = countX - 1;
if (countX >= MaxColumns - 1)
chigh = MaxColumns - 1;
else
chigh= countX + 1;
for (i = rlow; i <= rhigh; i++)
for (j = clow; j <= chigh; j++)
if ((pdisplay_table + i)->lines[j] == '*')
count++;
if ((pdisplay_table + countY)->lines[countX] == '*')
count--;
return count;
}
/* ======================================================= */
CopyMap()
{
int counterY,counterX;
for(counterY=1;counterY <= MaxRows;counterY++)
{
for(counterX=1;counterX <= 11;counterX++)
{
(pdisplay_table + counterY)->lines[counterX] = (pnew_display_table + counterY)->lines[counterX];
}
}
}
/* ======================================================= */
/* DRAWS ARRAY TO SCREEN */
void DRAWNSTRING(void)
{
WindowPtr theWindow;
int counterY,counterX,MoveX=50, MoveY=25 ;
theWindow = FrontWindow();
EraseRect(&(theWindow->portRect));
Update_LIFE_WINDOW(theWindow);/* CHEATING HERE BY UPDATING THE WINDOW TO HAVE CONTROLS REDRAWN */
MoveTo(MoveX,MoveY);
for(counterY=1;counterY <= MaxRows;counterY++)
{
for(counterX=1;counterX <= MaxColumns;counterX++)
{
DrawChar((pdisplay_table + counterY)->lines[counterX]);
}
MoveY +=15;
MoveTo(MoveX,MoveY);
}
}